c  
c  this program takes info file output and creates a data file in
c  modular flow model format.  the program works using seqnum,
c  not row and column.  the input file must contain seqnum and item
c
c  The program can also take unformatted model output and create formatted
c  model input
c
c  written by lebrooks Sept. 1991
c  Utah District
c  Water Resources Division
c
c  declare variables
c
      real array(40000),array2(200,200),item(40000),dryvalue
      integer seqnum(40000),mseqnum,nrow,ncol
      character*32 ofmt
      character*80 infile,ofile
      character*1 answer
      dimension text(4)     !text needed to read unformatted model output
      integer opt ! 0=write real values, 1=write integer values
c
c  prompt for and open input and output files
c
      write(6,*)'enter name of input file'
      read(5,'(a80)')infile
c
c  prompt to see if input file is unformatted model output
c
      write(6,*)'is input file unformatted model output (y/n)?'
      read (5,*)answer
      write(6,*)'enter name of output file'
      read(5,'(a80)')ofile
      write (6,*)'Enter output option (0=real values, 1=integer values)'
      read (5,*) opt
      if (opt.eq.0) then
         write (6,*)'enter real format of output file (in paren)'
      else
         write (6,*)'enter integer format of output file (in paren)'
      end if
      read(5,'(a32)')ofmt
c
      if (answer.eq.'y'.or.answer.eq.'Y') then
         open (unit=1,file=infile,status='old',form='unformatted')
      else
         open (unit=1,file=infile,status='old')
      end if
      open (unit=2,file=ofile,status='unknown')
c
c  if input file is unformatted model output, read the data
c
      if (answer.eq.'y'.or.answer.eq.'Y') then
         write(6,*)'enter value to use for dry cells'
         read(5,*)dryvalue
         write(6,*)'enter time step, period, and layer wanted'
         read(5,*)istp,iper,ilay
 8       read (1) KSTP,KPER,PERTIM,TOTIM,TEXT,NCOL,NROW,KLAY
         read(1) ((array2(i,j),j=1,ncol),i=1,nrow)
         if ((kstp.ne.istp).or.(kper.ne.iper).or.(klay.ne.ilay)) then
            go to 8
         end if
      else
c  if input file is info output
c  prompt for maximum row and columns
c
         write(6,*)'enter number of rows,columns'
         read(5,*)nrow,ncol
c
c  calculate maximum seqnum
c
         mseqnum = nrow * ncol
c
c  initialize array
c
         do 9 i=1,mseqnum
             array(i)=0
 9       continue
c
c  read the input file
c
         do 10 i = 1,mseqnum
            read(1,*,end=999)seqnum(i),item(i)
            array(seqnum(i)) = item(i)

 10      continue
 999     continue
c
c  assign 2-d array values based on row and column to get proper printout
c
         i = 1
         do 22 j = 1,nrow
            do 24 k = 1,ncol
               array2(j,k) = array(i)
               i = i + 1
 24         continue
 22      continue
      end if
c
c  print out values in assigned format
c
      do 34 i = 1,nrow
        if (opt.eq.0) then
           write(2,ofmt)(array2(i,j),j=1,ncol)
        else
           write(2,ofmt)(int(array2(i,j)),j=1,ncol)
        end if
 34   continue
c  close files
c
      close(1)
      close(2)
      stop
      end
